package com.plt.w2w;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Properties;
import java.sql.*;



public class Wardrobe {
	
	public Connection openConnection() {
		  Properties properties = new Properties();
		  properties.put("user", "root");
		  properties.put("password", "password");
		  properties.put("characterEncoding", "ISO-8859-1");
		  properties.put("useUnicode", "true");
		  String url = "jdbc:mysql://localhost/SECE";

		  try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  Connection c = null;
		try {
			c = DriverManager.getConnection(url, properties);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  return c;
		}
	
	public void create(ArrayList<Garment> garments,String wardrobeName){
		Connection conn = openConnection();
		Statement st;
		try {
			st = conn.createStatement();
			for(int i = 0;i<garments.size();i++){
				Garment g = (Garment) garments.get(i);
				String color = null;
				String shade = null;
				String maker = null;
				if(g.getColor() != ""){
					color = g.getColor();
				}
				if(g.getShade() != ""){
					shade = g.getShade();
				}
				if(g.getMaker() != ""){
					maker = g.getMaker();
				}
				String sql = "INSERT INTO wardrobes (Wardrobe_name,item,color,shade,maker,preference) " +
							"VALUES ('" + wardrobeName + "', '" + g.getType()  + "', '" + color + "', '" + 
						shade + "', '" + maker + "', " + g.getPreference() + ")";
				
				st.executeUpdate(sql);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
	}
	
	public ArrayList<Garment> checkInWardrobe(ArrayList<String> wardrobeNames){
		Iterator it = wardrobeNames.iterator();
		ArrayList<Garment> garments = new ArrayList<Garment>();
		ArrayList<Garment> temp = new ArrayList<Garment>();
		while(it.hasNext()){
			temp = checkInWardrobe((String)it.next());
			for(int i = 0; i<temp.size(); i++){
				garments.add(temp.get(i));
			}
		}
		return garments;
	}
	
	public ArrayList<Garment> checkInWardrobe(String wardrobeName){
		Connection conn = openConnection();
		ArrayList<Garment> garments = new ArrayList<Garment>();
		Statement st;
		Garment g;
		String type; 
		String color;
		String shade;
		String maker;
		String preference;
		try {
			st = conn.createStatement();
			String sql = "SELECT * FROM wardrobes WHERE Wardrobe_Name = '" + wardrobeName + "' " ;
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()){
				type = rs.getString(2);
				color = rs.getString(3);
				shade = rs.getString(4);
				maker = rs.getString(5);
				preference = rs.getString(6);
				g = new Garment(type,"as",maker,color,shade,preference);
				garments.add(g);
				
			}
		} catch(Exception e){
			e.printStackTrace();
		}
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return garments;
	}
	
	public ArrayList<Garment> checkInWardrobe(String wardrobeName, String attributeName, String value){
		Connection conn = openConnection();
		Statement st;
		ArrayList<Garment> garments = new ArrayList<Garment>();
		Garment g;
		String type; 
		String color;
		String shade;
		String maker;
		String preference;
		try {
			st = conn.createStatement();
			String sql = "SELECT * FROM wardrobes WHERE Wardrobe_Name = '" + wardrobeName + "' AND " + attributeName + " = '" + value + "'" ;
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()){
				type = rs.getString(2);
				color = rs.getString(3);
				shade = rs.getString(4);
				maker = rs.getString(5);
				preference = rs.getString(6);
				g = new Garment(type,"as",maker,color,shade,preference);
				garments.add(g);
			}
		} catch(Exception e){
			e.printStackTrace();
		}
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return garments;
	}
	
	public String chooseSnowJacket(){
		return "hey";
	}

}
